Skip to content

zeroize: add portable bulk data path for zeroizing slices - #1602

Merged
tarcieri merged 2 commits into
masterfrom
zeroize/portable-bulk-data-path
Nov 15, 2026
Merged

tarcieri merged 2 commits into
masterfrom
zeroize/portable-bulk-data-path

Conversation

@tarcieri

Copy link
Copy Markdown
Member

Adds a bulk data path to the internal volatile_set function, using 16-byte chunks for types whose size is 1, 2, 4, or 8 bytes. Remaining elements and other type sizes retain element-sized volatile writes.

Mutable chunks are produced using a copy of as_chunks_mut from Rust 1.88, vendored to preserve MSRV 1.85, with a TODO to replace the copy with the standard library method when MSRV permits.

The Zeroize impl for [Z] already requires DefaultIsZeroes, which has Default and Copy supertraits. This evaluates Default once, fills an array with copies of the returned value, then repeatedly uses volatile_write to copy that array into the destination chunks.

These array-valued volatile writes can lower to LLVM's llvm.memcpy intrinsic with its volatile flag set, allowing LLVM to select wider loads and stores when supported by the target.

Adds a bulk data path to the internal `volatile_set` function, using
16-byte chunks for types whose size is 1, 2, 4, or 8 bytes. Remaining
elements and other type sizes retain element-sized volatile writes.

Mutable chunks are produced using a copy of `as_chunks_mut` from Rust
1.88, vendored to preserve MSRV 1.85, with a TODO to replace the copy
with the standard library method when MSRV permits.

The `Zeroize` impl for `[Z]` already requires `DefaultIsZeroes`, which
has `Default` and `Copy` supertraits. This evaluates `Default` once,
fills an array with copies of the returned value, then repeatedly
uses `volatile_write` to copy that array into the destination chunks.

These array-valued volatile writes can lower to LLVM's `llvm.memcpy`
intrinsic with its volatile flag set, allowing LLVM to select wider
loads and stores when supported by the target.
@tarcieri
tarcieri requested a review from newpavlov November 14, 2026 18:18
@tarcieri

tarcieri commented Nov 14, 2026 •

Copy link
Copy Markdown
Member Author

Benchmark results on my local M3 Max:

slice/u8/15             time:   [4.8542 ns 4.9530 ns 5.0436 ns]
                        thrpt:  [2.7698 GiB/s 2.8205 GiB/s 2.8779 GiB/s]
                 change:
                        time:   [−3.3294% −1.4574% +0.5684%] (p = 0.16 > 0.05)
                        thrpt:  [−0.5652% +1.4789% +3.4440%]
                        No change in performance detected.
Found 3 outliers among 100 measurements (3.00%)
  3 (3.00%) high mild
slice/u8/16             time:   [761.31 ps 764.76 ps 768.99 ps]
                        thrpt:  [19.378 GiB/s 19.485 GiB/s 19.573 GiB/s]
                 change:
                        time:   [−83.371% −83.230% −83.078%] (p = 0.00 < 0.05)
                        thrpt:  [+490.95% +496.29% +501.36%]
                        Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
  5 (5.00%) high mild
  4 (4.00%) high severe
slice/u8/17             time:   [909.72 ps 911.95 ps 914.29 ps]
                        thrpt:  [17.317 GiB/s 17.361 GiB/s 17.404 GiB/s]
                 change:
                        time:   [−3.6789% −3.2679% −2.8406%] (p = 0.00 < 0.05)
                        thrpt:  [+2.9236% +3.3783% +3.8194%]
                        Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
  3 (3.00%) high mild
  2 (2.00%) high severe
slice/u8/32             time:   [920.79 ps 921.80 ps 922.91 ps]
                        thrpt:  [32.292 GiB/s 32.331 GiB/s 32.366 GiB/s]
                 change:
                        time:   [−89.613% −89.573% −89.535%] (p = 0.00 < 0.05)
                        thrpt:  [+855.55% +859.05% +862.78%]
                        Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
  2 (2.00%) low mild
  5 (5.00%) high mild
  5 (5.00%) high severe
slice/u8/63             time:   [5.4940 ns 5.5155 ns 5.5398 ns]
                        thrpt:  [10.591 GiB/s 10.638 GiB/s 10.680 GiB/s]
                 change:
                        time:   [−66.867% −66.713% −66.545%] (p = 0.00 < 0.05)
                        thrpt:  [+198.91% +200.42% +201.82%]
                        Performance has improved.
slice/u8/64             time:   [1.7356 ns 1.7378 ns 1.7406 ns]
                        thrpt:  [34.245 GiB/s 34.299 GiB/s 34.341 GiB/s]
                 change:
                        time:   [−89.754% −89.724% −89.692%] (p = 0.00 < 0.05)
                        thrpt:  [+870.12% +873.11% +875.95%]
                        Performance has improved.
Found 13 outliers among 100 measurements (13.00%)
  4 (4.00%) high mild
  9 (9.00%) high severe
slice/u8/65             time:   [1.7387 ns 1.7403 ns 1.7422 ns]
                        thrpt:  [34.746 GiB/s 34.784 GiB/s 34.817 GiB/s]
                 change:
                        time:   [−89.880% −89.853% −89.829%] (p = 0.00 < 0.05)
                        thrpt:  [+883.20% +885.53% +888.12%]
                        Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
  4 (4.00%) high mild
  4 (4.00%) high severe
slice/u8/128            time:   [2.7268 ns 2.7302 ns 2.7346 ns]
                        thrpt:  [43.593 GiB/s 43.663 GiB/s 43.717 GiB/s]
                 change:
                        time:   [−92.688% −92.655% −92.614%] (p = 0.00 < 0.05)
                        thrpt:  [+1254.0% +1261.4% +1267.7%]
                        Performance has improved.
Found 14 outliers among 100 measurements (14.00%)
  5 (5.00%) high mild
  9 (9.00%) high severe
slice/u8/4096           time:   [73.575 ns 73.802 ns 74.085 ns]
                        thrpt:  [51.491 GiB/s 51.688 GiB/s 51.848 GiB/s]
                 change:
                        time:   [−92.844% −92.815% −92.789%] (p = 0.00 < 0.05)
                        thrpt:  [+1286.7% +1291.8% +1297.5%]
                        Performance has improved.
Found 15 outliers among 100 measurements (15.00%)
  3 (3.00%) high mild
  12 (12.00%) high severe
slice/u8/65536          time:   [1.3633 µs 1.3931 µs 1.4197 µs]
                        thrpt:  [42.990 GiB/s 43.811 GiB/s 44.770 GiB/s]
                 change:
                        time:   [−91.626% −91.457% −91.299%] (p = 0.00 < 0.05)
                        thrpt:  [+1049.3% +1070.5% +1094.2%]
                        Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
  11 (11.00%) low mild
  1 (1.00%) high mild
slice/u8/fixed16        time:   [462.98 ps 463.93 ps 464.98 ps]
                        thrpt:  [32.047 GiB/s 32.120 GiB/s 32.185 GiB/s]
                 change:
                        time:   [−6.1544% −4.9800% −3.8111%] (p = 0.00 < 0.05)
                        thrpt:  [+3.9621% +5.2411% +6.5580%]
                        Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
  2 (2.00%) high mild
  5 (5.00%) high severe

@newpavlov

Copy link
Copy Markdown
Member

Seems fine, but for a better picture I think it's worth to include benchmarks for non-volatile write loop followed by optimizaton_barrier:

let buf: &mut [T] = ...;
for val in buf {
    *val = Default::default();
}
optimization_barrier(buf);

@tarcieri

Copy link
Copy Markdown
Member Author

I pushed up a side-by-side benchmark of that approach (I moved the Default::default invocation outside the loop as well).

It seems like the write_volatile bulk data path is marginally faster for small writes and has diminishing returns for larger ones (which could potentially be addressed with a secondary path for even larger writes that copies 64-byte cache line-sized values). Some of these operations are so fast though it can be hard to properly judge due to timing variability (e.g. some of the results are in picoseconds).

Results:

slice/u8/15             time:   [4.5010 ns 4.5326 ns 4.5642 ns]
                        thrpt:  [3.0608 GiB/s 3.0821 GiB/s 3.1037 GiB/s]
Found 14 outliers among 100 measurements (14.00%)
  14 (14.00%) high severe
slice/u8/write+barrier/15
                        time:   [3.9142 ns 3.9171 ns 3.9204 ns]
                        thrpt:  [3.5634 GiB/s 3.5664 GiB/s 3.5690 GiB/s]
Found 15 outliers among 100 measurements (15.00%)
  3 (3.00%) high mild
  12 (12.00%) high severe
slice/u8/16             time:   [802.27 ps 803.57 ps 804.97 ps]
                        thrpt:  [18.511 GiB/s 18.544 GiB/s 18.574 GiB/s]
Found 9 outliers among 100 measurements (9.00%)
  3 (3.00%) low mild
  2 (2.00%) high mild
  4 (4.00%) high severe
slice/u8/write+barrier/16
                        time:   [2.4824 ns 2.4853 ns 2.4885 ns]
                        thrpt:  [5.9879 GiB/s 5.9956 GiB/s 6.0028 GiB/s]
Found 15 outliers among 100 measurements (15.00%)
  6 (6.00%) high mild
  9 (9.00%) high severe
slice/u8/17             time:   [939.19 ps 940.01 ps 940.93 ps]
                        thrpt:  [16.826 GiB/s 16.843 GiB/s 16.858 GiB/s]
Found 7 outliers among 100 measurements (7.00%)
  4 (4.00%) high mild
  3 (3.00%) high severe
slice/u8/write+barrier/17
                        time:   [2.6284 ns 2.6329 ns 2.6386 ns]
                        thrpt:  [6.0002 GiB/s 6.0133 GiB/s 6.0237 GiB/s]
Found 12 outliers among 100 measurements (12.00%)
  5 (5.00%) high mild
  7 (7.00%) high severe
slice/u8/32             time:   [948.43 ps 949.16 ps 949.97 ps]
                        thrpt:  [31.372 GiB/s 31.399 GiB/s 31.423 GiB/s]
Found 8 outliers among 100 measurements (8.00%)
  4 (4.00%) high mild
  4 (4.00%) high severe
slice/u8/write+barrier/32
                        time:   [2.9726 ns 2.9747 ns 2.9771 ns]
                        thrpt:  [10.011 GiB/s 10.019 GiB/s 10.026 GiB/s]
Found 11 outliers among 100 measurements (11.00%)
  4 (4.00%) high mild
  7 (7.00%) high severe
slice/u8/63             time:   [5.4667 ns 5.4763 ns 5.4888 ns]
                        thrpt:  [10.690 GiB/s 10.714 GiB/s 10.733 GiB/s]
Found 18 outliers among 100 measurements (18.00%)
  16 (16.00%) high mild
  2 (2.00%) high severe
slice/u8/write+barrier/63
                        time:   [5.9082 ns 5.9200 ns 5.9351 ns]
                        thrpt:  [9.8859 GiB/s 9.9111 GiB/s 9.9309 GiB/s]
Found 11 outliers among 100 measurements (11.00%)
  5 (5.00%) high mild
  6 (6.00%) high severe
slice/u8/64             time:   [1.7565 ns 1.7591 ns 1.7619 ns]
                        thrpt:  [33.830 GiB/s 33.884 GiB/s 33.934 GiB/s]
Found 23 outliers among 100 measurements (23.00%)
  1 (1.00%) low severe
  14 (14.00%) low mild
  4 (4.00%) high mild
  4 (4.00%) high severe
slice/u8/write+barrier/64
                        time:   [1.5689 ns 1.5702 ns 1.5716 ns]
                        thrpt:  [37.926 GiB/s 37.960 GiB/s 37.992 GiB/s]
Found 6 outliers among 100 measurements (6.00%)
  2 (2.00%) low severe
  1 (1.00%) low mild
  3 (3.00%) high mild
slice/u8/65             time:   [1.8215 ns 1.8263 ns 1.8310 ns]
                        thrpt:  [33.062 GiB/s 33.146 GiB/s 33.233 GiB/s]
Found 1 outliers among 100 measurements (1.00%)
  1 (1.00%) high mild
slice/u8/write+barrier/65
                        time:   [1.7763 ns 1.7785 ns 1.7809 ns]
                        thrpt:  [33.992 GiB/s 34.038 GiB/s 34.079 GiB/s]
Found 22 outliers among 100 measurements (22.00%)
  12 (12.00%) low mild
  5 (5.00%) high mild
  5 (5.00%) high severe
slice/u8/128            time:   [2.7362 ns 2.7394 ns 2.7431 ns]
                        thrpt:  [43.458 GiB/s 43.517 GiB/s 43.568 GiB/s]
Found 3 outliers among 100 measurements (3.00%)
  1 (1.00%) high mild
  2 (2.00%) high severe
slice/u8/write+barrier/128
                        time:   [1.5672 ns 1.5715 ns 1.5757 ns]
                        thrpt:  [75.655 GiB/s 75.859 GiB/s 76.063 GiB/s]
Found 17 outliers among 100 measurements (17.00%)
  10 (10.00%) low severe
  2 (2.00%) low mild
  2 (2.00%) high mild
  3 (3.00%) high severe
slice/u8/4096           time:   [73.423 ns 73.489 ns 73.567 ns]
                        thrpt:  [51.853 GiB/s 51.908 GiB/s 51.955 GiB/s]
Found 7 outliers among 100 measurements (7.00%)
  2 (2.00%) high mild
  5 (5.00%) high severe
slice/u8/write+barrier/4096
                        time:   [33.507 ns 33.555 ns 33.615 ns]
                        thrpt:  [113.48 GiB/s 113.69 GiB/s 113.85 GiB/s]
Found 12 outliers among 100 measurements (12.00%)
  4 (4.00%) high mild
  8 (8.00%) high severe
slice/u8/65536          time:   [1.3871 µs 1.4081 µs 1.4297 µs]
                        thrpt:  [42.690 GiB/s 43.346 GiB/s 44.002 GiB/s]
Found 6 outliers among 100 measurements (6.00%)
  1 (1.00%) low severe
  5 (5.00%) low mild
slice/u8/write+barrier/65536
                        time:   [311.02 ns 315.89 ns 320.03 ns]
                        thrpt:  [190.72 GiB/s 193.21 GiB/s 196.24 GiB/s]
Found 15 outliers among 100 measurements (15.00%)
  15 (15.00%) low mild
slice/u8/fixed16        time:   [473.55 ps 474.22 ps 474.89 ps]
                        thrpt:  [31.378 GiB/s 31.422 GiB/s 31.467 GiB/s]
Found 10 outliers among 100 measurements (10.00%)
  3 (3.00%) low mild
  4 (4.00%) high mild
  3 (3.00%) high severe
slice/u8/write+barrier/fixed16
                        time:   [278.51 ps 286.40 ps 293.30 ps]
                        thrpt:  [50.805 GiB/s 52.029 GiB/s 53.504 GiB/s]
Found 22 outliers among 100 measurements (22.00%)
  22 (22.00%) high severe

All that said, I'm going to merge this as it's a definite improvement over the previous performance. I think there's some potential refactoring which could make it a little cleaner, but that can be done as a followup.

@tarcieri
tarcieri merged commit 26bfc25 into master Nov 15, 2026
29 checks passed
@tarcieri
tarcieri deleted the zeroize/portable-bulk-data-path branch November 15, 2026 15:57
tarcieri added a commit that referenced this pull request Nov 15, 2026
Followup to #1602 which migrates to safe slice-based fills for
initialized values, bypassing the slice-pointer-slice round trip which
was previously happening with the `volatile_set`-based API, which has
subsequently been replaced with `volatile_zero` for the non-slice based
use cases.

This can potentially be DRYed out further with `zeroize_flat_type`,
which I'd like to explore in a further followup.
tarcieri added a commit that referenced this pull request Nov 15, 2026
Followup to #1602 which migrates to safe slice-based fills for
initialized values, bypassing the slice-pointer-slice round trip which
was previously happening with the `volatile_set`-based API, which has
subsequently been replaced with `volatile_zero` for the non-slice based
use cases.

This can potentially be DRYed out further with `zeroize_flat_type`,
which I'd like to explore in a further followup.
tarcieri added a commit that referenced this pull request Nov 15, 2026
Followup to #1602 which migrates to safe slice-based fills for
initialized values, bypassing the slice-pointer-slice round trip which
was previously happening with the `volatile_set`-based API, which has
subsequently been replaced with `volatile_zero` for the non-slice based
use cases.

This can potentially be DRYed out further with `zeroize_flat_type`,
which I'd like to explore in a further followup.
tarcieri added a commit that referenced this pull request Nov 15, 2026
Adds an optimized byte slice zeroing function which uses a regular
`fill` followed by `optimization_barrier` on platforms with stable
`asm!` and falls back on the bulk volatile write implementation added
in #1602 on other platforms.

This approach should maintain the previous guarantees the purely
volatile write-based approach provided on all platforms, while also
providing the performance of the `optimization_barrier`-based approach
on platforms with `asm!`.

This function is public but also able to subsume the previous role of
the internal `volatile_zero` function added in #1603.
tarcieri added a commit that referenced this pull request Nov 15, 2026
Adds an optimized byte slice zeroing function which uses a regular
`fill` followed by `optimization_barrier` on platforms with stable
`asm!` and falls back on the bulk volatile write implementation added
in #1602 on other platforms.

This approach should maintain the previous guarantees the purely
volatile write-based approach provided on all platforms, while also
providing the performance of the `optimization_barrier`-based approach
on platforms with `asm!`.

This function is public but also able to subsume the previous role of
the internal `volatile_zero` function added in #1603.
tarcieri added a commit that referenced this pull request Nov 16, 2026
Adds an optimized byte slice zeroing function which uses a regular
`fill` followed by `optimization_barrier` on platforms with stable
`asm!` and falls back on the bulk volatile write implementation added
in #1602 on other platforms.

This approach should maintain the previous guarantees the purely
volatile write-based approach provided on all platforms, while also
providing the performance of the `optimization_barrier`-based approach
on platforms with `asm!`.

This function is public but also able to subsume the previous role of
the internal `volatile_zero` function added in #1603.
tarcieri added a commit that referenced this pull request Nov 16, 2026
Adds an optimized byte slice zeroing function which uses a regular
`fill` followed by `optimization_barrier` on platforms with stable
`asm!` and falls back on the bulk volatile write implementation added in
#1602 on other platforms.

This approach should maintain the previous guarantees the purely
volatile write-based approach provided on all platforms, while also
providing the performance of the `optimization_barrier`-based approach
on platforms with `asm!`.

This function is public but also able to subsume the previous role of
the internal `volatile_zero` function added in #1603.
@tarcieri tarcieri mentioned this pull request Nov 17, 2026
tarcieri added a commit that referenced this pull request Nov 17, 2026
## Added
- Optimized `zeroize_bytes` function (#1604)

## Changed
- Implement `optimization_barrier` using `core::hint::black_box` on
  targets without stable `asm!` (#1592)
- Optimized slice zeroization (#1602, #1603, #1607)
@djc

djc commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Good stuff, this yielded up to 3% improvements in rustls benchmarks that don't exercise any crypto!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants